/**
 * @Description
 * @Author LZL
 * @Date 2025/2/25
 */


package com.kcb.coursemanager;

import com.kcb.connectDB.connectDB;
import com.kcb.choose.choose2;

import javax.swing.*;
import java.awt.*;
import java.util.List; // 显式导入 java.util.List
import java.util.*;

public class printcourse extends JFrame {
    private JTextArea resultArea;
    private JButton backButton;
    private String username;

    public printcourse(String username) {
        this.username = username;
        setTitle("城科课程表管理系统:课程表打印功能");
        setSize(600, 450);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        ImageIcon icon = new ImageIcon("C:\\Users\\TBS\\IdeaProjects\\KCB\\image\\ccst.png");
        if (icon != null) {
            setIconImage(icon.getImage());
        } else {
            System.out.println("Failed to load icon image.");
        }

        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
        mainPanel.setBackground(new Color(240, 245, 255));

        // 标题
        JLabel titleLabel = new JLabel("课程表打印", JLabel.CENTER);
        titleLabel.setFont(new Font("宋体", Font.BOLD, 22));
        titleLabel.setForeground(new Color(50, 90, 180));
        mainPanel.add(titleLabel, BorderLayout.NORTH);

        // 课程显示区域
        resultArea = new JTextArea();
        resultArea.setFont(new Font("宋体", Font.PLAIN, 16));
        resultArea.setEditable(false);
        resultArea.setLineWrap(true);
        resultArea.setWrapStyleWord(true);
        JScrollPane scrollPane = new JScrollPane(resultArea);
        mainPanel.add(scrollPane, BorderLayout.CENTER);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        getContentPane().add(mainPanel);

        // 加载课程
        loadCourses();

    }

    private void loadCourses() {
        connectDB dbConnector = new connectDB();
        List<String[]> courses = dbConnector.getSortedCoursesByDayOfWeek(username);

        if (courses == null || courses.isEmpty()) {
            JOptionPane.showMessageDialog(this, "没有可显示的课程数据。", "提示", JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        Map<String, StringBuilder> weekMap = new LinkedHashMap<>();
        String[] daysOfWeek = {"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
        for (String day : daysOfWeek) {
            weekMap.put(day, new StringBuilder());
        }

        for (String[] course : courses) {
            if (course.length < 4) {
                System.out.println("数据格式错误: " + Arrays.toString(course));
                continue;
            }

            String[] dayAndTime = course[2].split(" ");
            String dayOfWeek = dayAndTime.length > 0 ? dayAndTime[0] : "未知";
            String time = dayAndTime.length > 1 ? dayAndTime[1] : "09:00"; // 默认时间

            if (!weekMap.containsKey(dayOfWeek)) {
                weekMap.put(dayOfWeek, new StringBuilder());
            }

            weekMap.get(dayOfWeek).append(course[0]).append(" - ")
                    .append(time).append(" - ")
                    .append("教师: ").append(course[1]).append(" - ")
                    .append("地点: ").append(course[3]).append("\n");
        }

        // 构建课程表显示文本
        StringBuilder result = new StringBuilder();
        for (String day : daysOfWeek) {
            if (weekMap.get(day).length() > 0) {
                result.append(day).append(":\n").append(weekMap.get(day)).append("\n");
            }
        }
        resultArea.setText(result.toString());
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new printcourse("exampleUser").setVisible(true));
    }
}
